<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<el-radio-group v-model="searchParams.state" size="small" class="item-gap" @change="onStateChange">
						<el-radio-button v-for="item in auditStateList" :key="item.id" :label="item.id">
							<el-badge v-if="item.id < 1 && item.count > 0" :value="item.count" :max="99" class="margin-right-10">
								<span class="margin-right-20">{{ item.name }}</span>
							</el-badge>
							<div v-else class="margin-right-10" style="padding-bottom: 2px;">{{ item.name }}</div>
						</el-radio-button>
					</el-radio-group>
				</div>
				<div class="flex-end-center-wrap">
					<jk-date-picker v-model="searchParams.belongDate" type="daterange" :disabled-future-time="true" placeholder="日期" size="small" class="search-bar-item-size4 margin-5-0 item-gap" clearable />
					<jk-search-input v-model="searchParams.productName" placeholder="品种名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-input v-model="searchParams.machineNameOrCode" placeholder="设备编号或名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table ref="jkTable" :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<jk-state-bar v-if="item.keyName === 'state'" :state="scope.row.state" :state-list="auditStateList"></jk-state-bar>
						<jk-state-bar v-else-if="item.keyName === 'openingState'" :state="scope.row.openingState" :state-list="stateList"></jk-state-bar>
						<span v-else-if="item.keyName === 'cycleUnit'">{{ transferCircleUnit(scope.row) }}</span>
						<span v-else>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
				<vxe-column title="操作" align="center" fixed="right" :width="164">
					<template slot-scope="scope">
						<div class="flex-center">
							<jk-delete-button class="item-gap" :permission-prefix="prefix" @on-confirm="onDeleteBtn(scope.row)"></jk-delete-button>
							<jk-button icon="el-icon-s-flag" :has-permission="getPermission([`${prefix}:update`])" :disabled="scope.row.state === 1" size="mini" type="primary" @click="onOpenBtn(scope.row)">处理</jk-button>
						</div>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<handleDialog
			:record-item="recordItem"
			:dialog-state="handleDialogState"
			@on-visible-change="onVisibleChangeOfDandleDialog"
			@on-confirm="onConfirmChangeOfDandleDialog"
		></handleDialog>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../mixins/getMenuColumn';
    import handlerCache from '../../../mixins/handler-cache';
    import {
        productionMachineShiftProductOutputDeleteById,
        productionMachineShiftProductOutputGetWaitOpenList
    } from '../../../api/production/order';
    import handleDialog from './handle-dialog';
    import permissionMixin from '../../../mixins/permission-mixin';
    import stateOptions from './state-options';
    export default {
        name: 'SupplementaryPlanList',
        components: {
            handleDialog
        },
        mixins: [calHeight, getMenuColumn, handlerCache, permissionMixin, stateOptions],
        data() {
            return {
                handleDialogState: false,
                prefix: 'production:machineProductShiftOutput',
                apiItem: {
                    'page': productionMachineShiftProductOutputGetWaitOpenList,
                    'delete': productionMachineShiftProductOutputDeleteById
                },
                recordItem: {},
                dataId: null,
                tableLoading: true,
                tableData: [],
                totalCount: 0,
                searchParams: {
                    pageNo: 1,
                    productName: '',
                    belongDate: [],
                    pageSize: pageConfig.pageSize,
                    machineNameOrCode: null,
                    state: 0
                }
            };
        },
        methods: {
            onOpenBtn(row) {
                this.recordItem = row;
                this.handleDialogState = true;
            },
            onConfirmChangeOfDandleDialog() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.handleDialogState = false;
                this.getListRequest();
            },
            onVisibleChangeOfDandleDialog(e) {
                this.handleDialogState = e;
            },
            onStateChange(e) {
                this.$set(this.searchParams, 'state', e);
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(pageIndex, pageSize) {
                this.$set(this.searchParams, 'pageNo', pageIndex);
                this.$set(this.searchParams, 'pageSize', pageSize);
                this.getListRequest();
            },
            getStateBarCountRequest() {
                const params = JSON.parse(JSON.stringify(this.searchParams));
                params.state = 0;
                this.apiItem.page(params).then(res => {
                    if (res.data.status === 200) {
                        this.auditStateList.forEach(x => {
                            if (x.id === 0) {
                                this.$set(x, 'count', res.data.count || 0);
                            }
                        });
                    } else {
                        this.auditStateList = this.auditStateList.map(x => {
                            this.$set(x, 'count', 0);
                            return x;
                        });
                    }
                });
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                const params = JSON.parse(JSON.stringify(this.searchParams));
                if (params.state === -1) params.state = '';
                this.apiItem.page(params).then(res => {
                    this.tableLoading = false;
                    this.getStateBarCountRequest();
                    if (res.data.status === 200) {
                        this.tableData = res.data.res || [];
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.totalCount = 1;
                this.getListRequest();
            },
            /** 删除按钮操作 */
            onDeleteBtn(row) {
                this.apiItem.delete({ id: row.id }).then((res) => {
                    if (res.data.status === 200) {
                        this.getListRequest();
                        this.$tipsBar(this, 'delete');
                    }
                }).catch(() => {});
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            async getDependentData() {
                await this.getMenuColumnListRequest();
                await this.getListRequest();
            }
        },
        mounted() {
            this.getDependentData();
        }
    };
</script>
